import { Injectable } from "@nestjs/common";
import { Patient, Patient as PatientModel, Prisma } from "@prisma/client";
import { BaseResponse, ErrorResponse, SuccessResponse } from "../types/types";
import { PrismaService } from "@app/prisma";

@Injectable()
export class PatientService {
  constructor(private prisma: PrismaService) {}

  async createPatient(data: Prisma.PatientCreateInput): Promise<BaseResponse> {
    try {
      const ans = await this.prisma.patient.create({
        data: data,
      });
      console.log(ans);
      return new SuccessResponse(ans);
    } catch (e) {
      return new ErrorResponse(0, `创建患者错误，${e.message}`);
    }
  }

  async getPatients(): Promise<BaseResponse> {
    try {
      const patients = await this.prisma.patient.findMany({});
      return new SuccessResponse(patients);
    } catch (err: any) {
      return new ErrorResponse(-1, "查询出错");
    }
  }

  async deletePatient(ids: number[]): Promise<BaseResponse> {
    try {
      console.log(`删除的患者ids为： ${ids}`);
      const ans = await this.prisma.patient.deleteMany({
        where: {
          patientIndex: {
            in: ids,
          },
        },
      });
      console.log(`删除的结果为：${ans}`);
      return new SuccessResponse(ans);
    } catch (e) {
      console.log(`删除出错:`, e);
      return new ErrorResponse(-1, "查询出错");
    }
  }

  async updatePatient(patient: PatientModel): Promise<BaseResponse> {
    try {
      const ans = await this.prisma.patient.update({
        data: patient,
        where: {
          patientIndex: patient.patientIndex,
        },
      });
      return new SuccessResponse(ans);
    } catch (e) {
      console.log(e);
    }
  }
}
